<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="galleryInfoTitle">
        <el-input
          v-model="queryParams.galleryInfoTitle"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="内容" prop="galleryInfoText">
        <el-input
          v-model="queryParams.galleryInfoText"
          placeholder="请输入内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="点赞数" prop="galleryInfoFabulous">
        <el-input
          v-model="queryParams.galleryInfoFabulous"
          placeholder="请输入点赞数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收藏数" prop="galleryInfoCollection">
        <el-input
          v-model="queryParams.galleryInfoCollection"
          placeholder="请输入收藏数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回复量" prop="galleryInfoReply">
        <el-input
          v-model="queryParams.galleryInfoReply"
          placeholder="请输入回复量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gallery:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gallery:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gallery:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gallery:info:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange" border @row-dblclick="getGalleryOriginal">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="图库ID" align="center" prop="galleryInfoId" />
      <el-table-column label="装修公司" align="center" prop="supplierInfo.shopsName" :show-overflow-tooltip="true"/>
      <el-table-column label="标题" align="center" prop="galleryInfoTitle" :show-overflow-tooltip="true"/>
      <el-table-column label="内容" align="center" prop="galleryInfoText" :show-overflow-tooltip="true"/>
      <el-table-column label="图片" align="center" prop="galleryInfoPic" :show-overflow-tooltip="true" />
      <el-table-column label="点赞数" align="center" prop="galleryInfoFabulous" />
      <el-table-column label="收藏数" align="center" prop="galleryInfoCollection" />
      <el-table-column label="回复量" align="center" prop="galleryInfoReply" />
      <el-table-column label="图库类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.galleryInfoType == '0'">建房图纸</span>
          <span v-else-if="scope.row.galleryInfoType == '1'">装修效果图</span>
          <span v-else-if="scope.row.galleryInfoType == '2'">3D设计</span>
        </template>
      </el-table-column>
      <el-table-column label="最后修改时间" align="center" prop="modifiedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifiedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gallery:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gallery:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改图库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺" prop="supplierId">
          <el-select v-model="form.supplierId">
            <el-option v-for="supplier in this.supplierList" :key="supplier.supplierId" :value="supplier.supplierId" :label="supplier.shopsName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="galleryInfoTitle">
          <el-input v-model="form.galleryInfoTitle" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容" prop="galleryInfoText">
          <el-input v-model="form.galleryInfoText" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="图片" prop="galleryInfoPic">
          <!--<el-input v-model="form.galleryInfoPic" placeholder="请输入图片" />-->
          <el-upload
            class="upload-demo"
            :http-request="upload"
            action="aaa"
            accept=".jpg,.png"
            :limit="1"
            :file-list="fileList"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :on-error="handleError"
            ref="upload">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">仅支持jpg,png格式,以最新上传为准</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="图库类型" prop="galleryInfoType">
          <el-select v-model="form.galleryInfoType" placeholder="请选择图库类型">
            <el-option label="建房图纸" value="0" />
            <el-option label="装修效果图" value="1" />
<!--            <el-option label="3D设计" value="2" />-->
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo, selectSupplierList,uploadImage } from "@/api/gallery/info";

export default {
  name: "Info",
  data() {
    return {
      //图片文件
      fileList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图库表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 店铺列表
      supplierList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        galleryInfoTitle: null,
        galleryInfoText: null,
        galleryInfoFabulous: null,
        galleryInfoCollection: null,
        galleryInfoReply: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        galleryInfoTitle: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        galleryInfoText: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        galleryInfoPic: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        galleryInfoFabulous: [
          { required: true, message: "点赞数不能为空", trigger: "blur" }
        ],
        galleryInfoCollection: [
          { required: true, message: "收藏数不能为空", trigger: "blur" }
        ],
        galleryInfoReply: [
          { required: true, message: "回复量不能为空", trigger: "blur" }
        ],
        galleryInfoType: [
          { required: true, message: "图库类型不能为空", trigger: "change" }
        ],
        modifiedTime: [
          { required: true, message: "最后修改时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询图库列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        console.log(response)
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 查看原文
    getGalleryOriginal(row){
      this.$router.push({path:"/galleryOriginal/original",query:{galleryInfoId:row.galleryInfoId}});
    },
    // 查询店铺列表
    getSupplierList(){
      selectSupplierList().then(response => {
        this.supplierList = response.data;
      })
    },
    // 表单重置
    reset() {
      this.form = {
        galleryInfoId: null,
        supplierId: null,
        galleryInfoTitle: null,
        galleryInfoText: null,
        galleryInfoPic: null,
        galleryInfoFabulous: null,
        galleryInfoCollection: null,
        galleryInfoReply: null,
        galleryInfoType: null,
        modifiedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.galleryInfoId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getSupplierList();
      this.fileList = [];
      this.open = true;
      this.title = "添加图库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getSupplierList();
      const galleryInfoId = row.galleryInfoId || this.ids
      getInfo(galleryInfoId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改图库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.galleryInfoId != null) {
            updateInfo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addInfo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const galleryInfoIds = row.galleryInfoId || this.ids;
      this.$confirm('是否确认删除图库编号为"' + galleryInfoIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(galleryInfoIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有图库数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },

    //文件上传
    upload(param){
      let file = param.file;
      let fileType = file.type;
      let isLt2M = file.size / 1024 / 1024 < 500;
      if (!isLt2M) {
        this.$message.error("上传大小大小必须小于500M");
        this.fileList=[];
        return;
      }
      let form = new FormData();
      form.append("file", file);
      uploadImage(form).then((res)=>{
        if(res.code==200){
          this.form.galleryInfoPic = "http://51jhf.com/profile/xiazai"+res.filePath;
        }
        console.log(res);
      })
    },
    //移除文件之前
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    //移除
    handleRemove(file, fileList){
      this.form.galleryInfoPic = '';
      this.fileList=[];
    },
    //上传异常
    handleError(err, file, fileList){
      this.$message.error("上传失败");
    },
  }
};
</script>
